﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace TeZhanDui
{
    class GameTimer
    {
        public GAME_STATE state { get; set; }
        Timer countDownTimer = null;
        public int distributeCount { get; set; }
        public int offendCount { get; set; }
        public int defendCount { get; set; }
        public int compareCount { get; set; }
        public int endCount { get; set; }
        
        public GameTimer(Game game)
        {
            state = GAME_STATE.DISTRIBUTE;
            countDownTimer = new Timer(CountDownTimerMethod, game, 1000, 1000);
            distributeCount = 3;
            offendCount = 8;
            defendCount = 10;
            compareCount = 18;
            endCount = 12;
        }

        void CountDownTimerMethod(object o)
        {
            Game game = o as Game;
            lock (game)
            {
                switch (state)
                {
                    case GAME_STATE.DISTRIBUTE:
                        distributeCount--;
                        if (distributeCount <= 0)
                        {
                            StartOffend();
                            game.CaculateCards();
                        }
                        break;
                    case GAME_STATE.OFFEND:
                        offendCount--;
                        if (offendCount <= 0)
                        {
                            StartDefend();
                            game.CaculateCards();
                        }
                        break;
                    case GAME_STATE.DEFEND:
                        defendCount--;
                        if (defendCount <= 0)
                        {
                            StartCompare();
                            game.CaculateCards();
                        }
                        break;
                    case GAME_STATE.COMPARE:
                        compareCount--;
                        if (compareCount <= 0)
                        {
                            StartOffend();
                            game.CaculateCards();
                            /*if (game.state == GAME_STATE.END)
                            {
                                StartEnd();
                            }*/
                        }
                        break;
                    case GAME_STATE.END:
                        endCount--;
                        if (endCount <= 0)
                        {
                            state = GAME_STATE.CLOSE;
                            game.state = GAME_STATE.CLOSE;
                            countDownTimer.Dispose();
                            countDownTimer = null;
                            game.room.state = ROOM_STATE.WAITING;
                            game.room.playerCount = 0;
                            game.room.game = null;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        void ResetCount()
        {
            distributeCount = 3;
            offendCount = 8;
            defendCount = 10;
            compareCount = 18;
            endCount = 12;
        }

        public void StartOffend()
        {
            state = GAME_STATE.OFFEND;
            ResetCount();
        }

        public void StartDefend()
        {
            state = GAME_STATE.DEFEND;
            ResetCount();
        }

        public void StartCompare()
        {
            state = GAME_STATE.COMPARE;
            ResetCount();
        }

        public void StartEnd()
        {
            state = GAME_STATE.END;
            ResetCount();
        }
    }
}
